package com.redijedi.tapestry5.pages;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.UUID;

import org.apache.tapestry.annotations.Component;
import org.apache.tapestry.annotations.SetupRender;

import com.redijedi.tapestry5.TreeNodeModel;
import com.redijedi.tapestry5.components.Tree;
import com.redijedi.tapestry5.test.Thing;

/**
 * @author torr
 * 
 */
public class TreeTestPage {

	@SuppressWarnings("all")
	@Component(parameters = { "model=model" })
	private Tree tree;

	private Thing hierarchicalThing;

	public TreeNodeModel getModel() {
		return new TreeModelImpl(hierarchicalThing);
	}

	@SetupRender
	void populateThings() {
		hierarchicalThing = new Thing("1").addChild(new Thing("2").addChild(new Thing("3")));
	}

	/**
	 * Internal TreeModel implementation.
	 * 
	 * @author torr
	 * 
	 */
	private class TreeModelImpl implements TreeNodeModel {

		final String id;

		final Collection<TreeModelImpl> nodes;

		final Object value;

		TreeModelImpl(Thing thing) {
			id = thing.getId();
			value = thing;
			nodes = new ArrayList<TreeModelImpl>();
			for (Thing t : thing.getChildren()) {
				nodes.add(new TreeModelImpl(t));
			}
		}

		public String getId() {
			return id;
		}

		public Map<String, String> getAttributes() {
			return null;
		}

		public Object getValue() {
			return value;
		}

		public Iterable<TreeModelImpl> getChildren() {
			return nodes;
		}

	}
}
